package com.csglxt.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @ClassName: DateUtil
 * @Description: 日期处理类
 * @author Delver_Si
 * @date 2015-5-28 下午11:56:03
 * 
 */

public class DateUtil {

    public static String getCurDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static java.util.Date sqldatetoutil(java.sql.Date date) {
        java.util.Date d = new java.util.Date(date.getTime());
        return d;
    }

    @SuppressWarnings("unused")
    public static java.sql.Date utildatetosql(java.util.Date utilDate) {
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        java.sql.Time sTime = new java.sql.Time(utilDate.getTime());
        java.sql.Timestamp stp = new java.sql.Timestamp(utilDate.getTime());
        return sqlDate;
    }

    /**
     * 将一个字符串转换成日期格式
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static Date toDate(String date, String pattern) {
        if (("" + date).equals("")) {
            return null;
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date newDate = new Date();
        try {
            newDate = sdf.parse(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return newDate;
    }

    /**
     * 把日期转换成字符串型
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static String toString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        String dateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            dateString = sdf.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateString;
    }

    public static java.sql.Date getCursqlDate() {
        java.util.Date curutildate = toDate(DateUtil.getCurDate(),
                "yyyy-MM-dd HH:mm:ss");
        return utildatetosql(curutildate);

    }

}
